package com.globaldom.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.Research;
import com.globaldom.model.Science;
import com.globaldom.utils.Constants;
import com.globaldom.utils.PMF;
import com.globaldom.utils.ScienceUtil;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/getallscience")
public class ScienceGetAllController {
	public ScienceGetAllController() {
	}

	private static final Logger log = Logger.getLogger(ScienceGetAllController.class.getName());

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getall", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Object> getAllSales(@RequestParam int myLevel, HttpSession session, HttpServletRequest request) {
		Map<String, Object> map;
		map = new HashMap<String, Object>();
		String personID = SessionManager.getID(request, session);
		if (null == personID) {
			// map = new HashMap<String, String>();
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
		}

		List<Science> science = new ArrayList<Science>();

		// List<Science> science = MemCacheManager.getAllScience();
		//
		// if (null != science) {
		// map.put("syfy", science);
		// // return map;
		// } else {
		// science = new ArrayList<Science>();
		PersistenceManager pmf = PMF.get().getPersistenceManager();
		try {
			// FIXME add the minLevel-5 clause here
			// String sql = "Select s from Science s order by ";
			Query q = pmf.newQuery(Science.class);
			q.setOrdering("minLevel");
			q.setRange(myLevel, myLevel + 10);

			science = (List<Science>) q.execute();
			if (science.size() < 1) {
				ScienceUtil sc = new ScienceUtil();
				sc.populateScience();
			}
			map.put("syfy", science);

			// MemCacheManager.putScience(science);
		} catch (Exception ex) {
			log.severe("Unable to get all science " + ex.getLocalizedMessage());
			ex.printStackTrace();
		}

		// }
		// Caching problem. Commenting it out! FIXME
		List<Research> researches = null;// MemCacheManager.getResearch(personID);
		// if(null!=researches)
		// {
		// map.put("myresearch", researches);
		// }
		// else
		// {

		try {
			// String sql = "Select r from  r where ";
			Query q = pmf.newQuery(Research.class, "personID==pid");
			q.declareParameters("String pid");
			researches = (List<Research>) q.execute(personID);
			ArrayList<Research> al = new ArrayList<Research>(researches);
			map.put("myresearch", al);
			// if(researches.size()>0)
			// MemCacheManager.putResearch(personID, researches);
		} catch (Exception ex) {
			// log.severe("Unable to get all science " +
			// ex.getLocalizedMessage());
			ex.printStackTrace();
		} finally {
			pmf.close();
		}

		// }
		return map;

	}
}
